GBML manual / Stacks

stack_contains

True if any item in target equals value, false otherwise (including when target is empty). Nothing in target changes, and no item is taken off.

Syntax

stack_contains(target, value) -> bool

Arguments

NameType
targetstack
valueelement

Returns

bool

Description

true if any item in target, a stack, equals value, false otherwise (including when target is empty).

It only looks. Nothing is taken off the stack and nothing is added, so the top item is still the top item afterwards. It answers yes or no and never says where the value is, because a stack has no places to count - only a top.

This is the stack's own name for the question list_contains asks a list, queue_contains asks a queue and set_contains asks a set. Each container uses its own name so it is always clear which kind of thing you are asking.

What counts as equal

Numbers are equal when they are the same number, and text is equal when every letter is the same.

The longer the stack, the longer it takes

The search starts at the bottom and looks at one item after another until it finds a match, so a stack that holds more takes longer, and a search that finds nothing looks at every item. A stack that is only for pushing and popping never needs this; reach for it when you want to avoid pushing the same thing twice.

What happens in the awkward cases

You do thisWhat happens
search for a value that is not therefalse
search for a value that is there more than oncetrue
search an empty stackfalse
give it a listrefused when checked. A list has its own, list_contains

Example

/// @type {stack<int>}
var history = [];
stack_push(history, 4);
stack_push(history, 8);
var has8 = stack_contains(history, 8);    // true
var has99 = stack_contains(history, 99);  // false
if (not stack_contains(history, 12)) {
    stack_push(history, 12);              // only pushed if it is not already in the pile
}

On the GBA

The same left-to-right search list_contains makes (find, the GBA module's own alias for std::find) over the vector the stack is kept in - O(n) in how many items target holds. It only answers yes or no; it does not say where the value is, because a stack has no places to count.

See also: stack_pushstack_popstack_peekstack_emptylist_containsStacksData containers